<%=render 'layouts/header'%>

<div class="container plat show app-host-container">

  <div class="visible-md visible-lg pull-right">

    <% if can? :destroy, @plat %>
      <%=link_to '删除', app_plat_path(@app, @plat), data: {:confirm => 'Are you sure?'}, class:"btn-danger", :method => :delete %>
    <% end %>

    <% if can? :update, @plat %>
      <%=link_to '修改', edit_app_plat_path(@app, @plat), class:"btn-info" %>
    <% end %>

  </div>

  <ul class="nav nav-tabs" id="nav_ul">
      <%@plats.each_with_index do |plat,index|%>
        <% if plat.id == params[:id].to_i %>
          <li data-id="<%=plat.id%>" role="presentation" class="active nav-tab-item"><%=link_to plat.name, app_plat_path(@app,plat) %></li>
        <% else %>
          <li data-id="<%=plat.id%>" role="presentation" class="nav-tab-item"><%=link_to plat.name, app_plat_path(@app,plat) %></li>
        <% end %>
      <% end %>
      <% if can? :create, Plat %>
        <li role="presentation" class="visible-md visible-lg nav-add-btn" id="nav_add_btn"><%=link_to '+', new_app_plat_path %></li>
      <% end %>
  </ul>

  <div class="row">
    <%=render partial: "cell", collection: @pkgs, as: :pkg %>
  </div>
  <div class="paginate">
    <%= paginate @pkgs %>
  </div>

  <div class="plat-info">
    <span>
      平台: <%=@plat.plat_des_name%> 包数: <%=@plat.pkgs.count%> 
      <% if @plat.bundle_id.present? %>
       包名: <%=@plat.bundle_id%>  
      <% end %>
    </span>
  </div>

  <% if can? :create, Pkg %>
    <div class="visible-md visible-lg pull-right">
      <%=link_to '上传', new_plat_pkg_path(@plat), class:"btn btn-primary ui-plat" %>
    </div>
  <% end %>


</div>

<script type="text/javascript">
  $(function(){
    //修改循序
    <% if can? :sort, Plat %>
      var add_btn = null;
      var list = document.getElementById("nav_ul");
      Sortable.create(list,
        {filter:".nav-add-btn",
          onStart: function (evt) {
            console.log('onStart')
            add_btn = $("#nav_add_btn")
            $(add_btn).remove()
          },
          onEnd: function (evt) {
            console.log('onEnd')
            var ids = $(".nav-tab-item").map(function(){
              return $(this).data("id")
            }).get()

            $.ajax({
              url: '/api/plat/sort',
              type: 'PUT',
              data: {ids: ids.join(','),app_id:<%=params[:app_id]%>},
            })
            .fail(function() {
              alert("修改顺序失败")
              location.reload();
            })

            $("#nav_ul").append(add_btn)
          }}
        );
    <% end %>
  })
</script>